package cn.kgc.kmall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.kgc.kmall.product.entity.CategoryBrandRelationEntity;
import cn.kgc.kmall.product.service.CategoryBrandRelationService;
import cn.kgc.common.utils.PageUtils;
import cn.kgc.common.utils.R;



/**
 * 品牌分类关联
 *
 * @author chenkaifeng
 * @email 403707110@qq.com
 * @date 2022-04-06 15:13:25
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     * (关联关系表中有冗余列，因此使用自动生成的save方法不满足要求，因此需要自定义保存方法)
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
//		categoryBrandRelationService.save(categoryBrandRelation);
        categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     *获取当前品牌关联的所有分类列表
     * (品牌和分类的关系表存储在pms_category_brand_relation表中,其中brand_name和catelog_name是冗余列)
     **/
    @GetMapping("/catelog/list")
    //@RequiresPermissions（"product:categorybrandrelation:list"）
    public R cateloglist(@RequestParam("brandld")Long brandld){
        List<CategoryBrandRelationEntity> data=categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandld));

        return R.ok().put("data",data);

    }



}
